package rpg.visual.mapEditor;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import pgui.PTableRenderer;
import square.data.CreatureData;
import square.data.DataGroup;
import square.data.ItemData;

public class VisualList extends JPanel {
	private static final long serialVersionUID = 1L;

	private VisualMapEditor visualMap;
	private VisualOptions visualOptions;

	// Swing Components
	private JComboBox<String> swingCombobox;
	private JTable swingTable;
	private TableModel swingCreatureModel, swingItemModel;

	// Constructor
	public VisualList(VisualMapEditor visualMap,
			VisualOptions visualOptions) {
		this.setVisualMap(visualMap);
		this.setVisualOptions(visualOptions);

		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setMinimumSize(new Dimension(256, 0));
		this.setPreferredSize(new Dimension(256, Short.MAX_VALUE));
		this.setMaximumSize(new Dimension(256, Short.MAX_VALUE));

		// Combobox
		String comboboxValues[] = { "Creatures", "Items" };
		swingCombobox = new JComboBox<String>(comboboxValues);
		swingCombobox.setMaximumSize(new Dimension(Short.MAX_VALUE, 32));
		swingCombobox.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent event) {
				String selection = swingCombobox.getSelectedItem().toString();
				if (selection.equals("Creatures")) {
					swingTable.setModel(swingCreatureModel);
				} else if (selection.equals("Items")) {
					swingTable.setModel(swingItemModel);
				}
				setTableAttributes();
			}
		});
		this.add(swingCombobox);

		// Combobox To Table
		this.add(Box.createRigidArea(new Dimension(0, 6)));

		// Table
		swingTable = new JTable();
		swingTable.setRowHeight(32);
		swingTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		JScrollPane swingTableScrollPane = new JScrollPane(swingTable);
		swingTableScrollPane.setPreferredSize(new Dimension(Short.MAX_VALUE,
				Short.MAX_VALUE));
		this.add(swingTableScrollPane);
	}

	public void setTableAttributes() {
		swingTable.getColumnModel().getColumn(0).setMaxWidth(64);
		swingTable.getColumnModel().getColumn(0)
				.setCellRenderer(new PTableRenderer());

		swingTable.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {

					@Override
					public void valueChanged(ListSelectionEvent event) {
						if (event.getValueIsAdjusting())
							return;

						VisualMapEditor map = getVisualMap();

						if (swingTable.getSelectedRow() == -1) {
							map.setWorkAction("select");
							map.setWorkType(null);
							visualOptions.getSwingSelectButton().setSelected(true);
							return;
						} else {
							map.setWorkAction("insert");
						}
						
						visualOptions.getSwingInsertButton().setSelected(true);

						if (swingTable.getModel() == swingCreatureModel) {
							String name = swingCreatureModel.getValueAt(
									swingTable.getSelectedRow(), 1).toString();

							map.setWorkName(name);
							map.setWorkType("creature");
						} else if (swingTable.getModel() == swingItemModel) {
							String name = swingItemModel.getValueAt(
									swingTable.getSelectedRow(), 1).toString();

							map.setWorkName(name);
							map.setWorkType("item");
						}
					}
				});
	}

	@SuppressWarnings("serial")
	public void loadSwingModels() {
		String[] columns = { "Image", "Name" };

		// Creatures
		ArrayList<CreatureData> creatures = DataGroup.getCreatures();
		swingCreatureModel = new DefaultTableModel(columns, creatures.size()) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};

		for (int i = 0; i < creatures.size(); i++) {
			CreatureData it = creatures.get(i);
			if (it.getSprite() == null
					|| it.getSprite().getFrames().size() == 0) {
				swingCreatureModel.setValueAt("Invalid img", i, 0);
			} else {
				swingCreatureModel.setValueAt(it.getSprite().getFrame(0), i, 0);
			}
			swingCreatureModel.setValueAt(it.getName(), i, 1);
		}

		// Items
		ArrayList<ItemData> items = DataGroup.getItems();
		swingItemModel = new DefaultTableModel(columns, items.size()) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};

		for (int i = 0; i < items.size(); i++) {
			ItemData it = items.get(i);
			if (it.getSprite() == null
					|| it.getSprite().getFrames().size() == 0) {
				swingItemModel.setValueAt("Invalid img", i, 0);
			} else {
				swingItemModel.setValueAt(it.getSprite().getFrame(0), i, 0);
			}
			swingItemModel.setValueAt(it.getName(), i, 1);
		}

		// Start
		getSwingTable().setModel(swingCreatureModel);
		setTableAttributes();
	}

	// Get & Set
	public VisualMapEditor getVisualMap() {
		return visualMap;
	}

	public void setVisualMap(VisualMapEditor visualMap) {
		this.visualMap = visualMap;
	}

	public VisualOptions getVisualOptions() {
		return visualOptions;
	}

	public void setVisualOptions(VisualOptions visualOptions) {
		this.visualOptions = visualOptions;
	}

	// Get & Set Swing
	public JComboBox<String> getSwingCombobox() {
		return swingCombobox;
	}

	public void setSwingCombobox(JComboBox<String> swingCombobox) {
		this.swingCombobox = swingCombobox;
	}

	public JTable getSwingTable() {
		return swingTable;
	}

	public void setSwingTable(JTable swingTable) {
		this.swingTable = swingTable;
	}

	public TableModel getSwingCreatureModel() {
		return swingCreatureModel;
	}

	public void setSwingCreatureModel(TableModel swingCreatureModel) {
		this.swingCreatureModel = swingCreatureModel;
	}

	public TableModel getSwingItemModel() {
		return swingItemModel;
	}

	public void setSwingItemModel(TableModel swingItemModel) {
		this.swingItemModel = swingItemModel;
	}
}
